package com.qf.controller;

import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/groupon")
public class AdminGrouponController {

    @Autowired
    DtsGrouponRulesService grouponRulesService;

    @PostMapping("/update")
    public Object update(@RequestBody DtsGrouponRules grouponRules){
        grouponRulesService.update(grouponRules);

        return ResponseUtil.ok();
    }

    @PostMapping("/create")
    public Object create(@RequestBody Map<String, Object> map){
        String discount = (String) map.get("discount");
        Integer discountMember = Integer.parseInt((String) map.get("discountMember"));
        String expireTime = (String) map.get("expireTime");
        Long goodsId = Long.valueOf((String)map.get("goodsId")) ;
        String goodsName = (String) map.get("goodsName");

        try {
            grouponRulesService.insert(discount,discountMember,expireTime,goodsId,goodsName);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail(-1,"没有此商品");
        }

        return ResponseUtil.ok(map);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGrouponRules grouponRules){
        grouponRulesService.delete(grouponRules);

        return ResponseUtil.ok();
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       Integer goodsId){
        Map<String,Object> data = grouponRulesService.findByPage(page,limit,sort,order,goodsId);

        return ResponseUtil.ok(data);
    }
}
